package com.thinkgem.jeesite.modules.sys.utils;

import org.joda.time.DateTime;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author administrator 日期时间公共类 使用Joda Time进行时间的计算，最后转换为java.util.Date
 */
public class DateTimeUtils {


	private static final SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	public static final int TIME_TYPE_MONTH = 0;
	public static final int TIME_TYPE_DAY = 1;
	public static final int TIME_TYPE_HOUR = 2;
	public static final int TIME_TYPE_MINUTE = 3;
	public static final int TIME_TYPE_SECOND = 4;

	/**
	 * 在指定时间上加上或减去固定时间
	 * */
	public static Date addTime(Date date, int time, int timeType) {
		DateTime dateTime = new DateTime(date);
		DateTime newDate = null;
		if (timeType == TIME_TYPE_MONTH) {// 月
			newDate = dateTime.plusMonths(time);
		} else if (timeType == TIME_TYPE_DAY) {// 日
			newDate = dateTime.plusDays(time);
		} else if (timeType == TIME_TYPE_HOUR) {// 时
			newDate = dateTime.plusHours(time);
		}else if (timeType == TIME_TYPE_MINUTE){ //分
			newDate = dateTime.plusMinutes(time);
		}else if(timeType == TIME_TYPE_SECOND){
			newDate = dateTime.plusSeconds(time);
		}
		return newDate.toDate();
	}

	/**
	 * 在指定时间上减去固定时间
	 * */
	public static Date minusTime(Date date, int time, int timeType) {
		DateTime dateTime = new DateTime(date);
		DateTime newDate = null;
		if (timeType == TIME_TYPE_MONTH) {// 月
			newDate = dateTime.minusMonths(time);
		} else if (timeType == TIME_TYPE_DAY) {// 日
			newDate = dateTime.minusDays(time);
		} else if (timeType == TIME_TYPE_HOUR) {// 时
			newDate = dateTime.minusHours(time);
		}else if (timeType == TIME_TYPE_MINUTE){ //分
			newDate = dateTime.minusMinutes(time);
		}else if(timeType == TIME_TYPE_SECOND){
			newDate = dateTime.minusSeconds(time);
		}
		return newDate.toDate();
	}

	public static String format(long date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(date);
		return convertDate2String("yyyy-MM-dd HH:mm:ss.S",calendar.getTime());
	}

	/**
	 * 在当前系统时间上加上或减去固定月份
	 * */
	public static Date addMonths(int month) {
		DateTime dateTime = new DateTime();
		return dateTime.plusMonths(month).toDate();
	}

	/**
	 * 获取当前日期
	 */
	public static Date getCurrentDate() {
		Calendar calendar = Calendar.getInstance();
		return calendar.getTime();
	}

	public static Date getToday(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		return calendar.getTime();
	}

	public static Date getTomorrow(){
		return addTime(getToday(), 1 , TIME_TYPE_DAY);
	}

	/**
	 * 在指定日期上加上或减去固定月份
	 * */
	public static Date addMonths(Date date, int month) {
		DateTime dateTime = new DateTime(date);
		return dateTime.plusMonths(month).toDate();
	}

	/**
	 * 将日期转换为字符串，如果未指定格式串，使用默认格式串
	 * */
	public static String convertDate2String(String format, Date date) {

		if (format == null)
			return sdf.format(date);

		SimpleDateFormat dateFormat = null;
		try {
			dateFormat = new SimpleDateFormat(format);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return dateFormat.format(date);
	}

	/**
	 * 将字符串转换为日期，如果未指定格式串，使用默认格式串
	 * */
	public static Date convertString2Date(String format, String dateStr) {

		Date date = null;

		try {
			if (format == null) {
				date = sdf.parse(dateStr);
			} else {
				SimpleDateFormat dateFormat = new SimpleDateFormat(format);
				date = dateFormat.parse(dateStr);
			}

		} catch (Exception e) {
			 e.printStackTrace();
		}

		return date;
	}

	/**
	 * 获取缓存到期时间
	 * */
	public static Date getExpiredCacheDate(int minutes) {
		DateTime dateTime = new DateTime();
		return dateTime.plusMinutes(minutes).toDate();
	}

	public static String getYYYY_MM_DD_HH_MM_SS(Date date) {
		return convertDate2String("yyyy-MM-dd HH:mm:ss", date);
	}


	public static Date getDateYYYY_MM_DD_HH_MM_SS(String date) {
		return convertString2Date("yyyy-MM-dd HH:mm:ss", date);
	}


	public static String getCurrentDateTimeStr() {
		return convertDate2String("yyyy-MM-dd HH:mm:ss", getCurrentDate());
	}



	//kdq
	//2015-12-15
	public static String getyyyymmddhhmmss() {
		return convertDate2String("yyyyMMddHHmmss", getCurrentDate());
	}

	//kdq
	//2016-01-13
	public static String getCurrentDateStr() {
		return convertDate2String("yyyy-MM-dd", getCurrentDate());
	}

	/**
	 * 昨天
	 * 
	 * @return
	 */
	public static Date getYesterDay() {
		return addTime(getCurrentDate(), -1, TIME_TYPE_DAY);
	}

	/**
	 * 获取上个月
	 * 
	 * @return
	 */
	public static Date getPreMonth() {
		return addTime(getCurrentDate(), -1, TIME_TYPE_MONTH);
	}

	public static Date getFirstDayInCurMonth() {
		return getFirstDayInMonth(getCurrentDate());
	}
	
	public static Date getFirstDayInMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}
	
	public static Date getLastDayInCurMonth(){
		Date nextMonth = addTime(getFirstDayInCurMonth(), 1, TIME_TYPE_MONTH);
		return addTime(nextMonth, -1, TIME_TYPE_DAY);
	}


    /**
	 * 获得两个日期之间相差天数.
     * @param date1
     * @param date2
     * @return
     */
    public static int diffDays(Date date1, Date date2) {
        return (int) (Math.abs(date1.getTime() - date2.getTime()) / 1000 / 60 / 60 / 24);
    }

    /**
	 * 判断输入的时间大于当前时间还是小雨当前时间
	 * @return   true  or  false
	 * @author 孔德强
	 * @date 2015-12-02
	 */
	public static boolean getSmallOrLargeTime(Date d1) {
		Date now=new Date();
		try {
			return now.before(d1);
		}catch (Exception e){
			return false;
		}
	}

	/**
	 * 获取当前时间的小时开始
	 * @return Date
	 */
	public static Date getNowHourStart(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);

		return calendar.getTime();
	}

	/**
	 * 获取当前时间的小时结束
	 * @return Date
	 */
	public static Date getNowHourEnd(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MINUTE,59);
		calendar.set(Calendar.SECOND,59);

		return calendar.getTime();
	}

	/**
	 * 获取本周的周日的日期
	 * @return
	 */
	public static Date getNowWeekEnd(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		calendar.add(Calendar.WEEK_OF_YEAR, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	/**
	 * 获取本周的周一的日期
	 * @return
	 */
	public static Date getNowWeekStart(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取本月第一天日期
	 * @return
	 */
	public static Date getNowMonthStart(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE,calendar.getActualMinimum(Calendar.DATE));
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取本月最后一天日期
	 * @return
	 */
	public static Date getNowMonthEnd(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DATE));
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);

		return calendar.getTime();
	}
	public static void main(String[] args) {
//		System.out.println(convertDate2String("yyyy-MM-dd", getYesterDay()));
//		System.out.println(convertDate2String("yyyy-MM-dd", getPreMonth()));
//		System.out.println(convertDate2String("yyyy-MM-dd",getFirstDayInCurMonth()));
//		System.out.println(convertDate2String("yyyy-MM-dd",getLastDayInCurMonth()));
//
//		System.out.println(getToday());
//		System.out.print(getTomorrow());
//


//		System.err.println(getSmallOrLargeTime(minusTime(new Date(),-1,1)));
//
//		System.err.println("====="+Math.floor(8/2));
//		Calendar calendar = Calendar.getInstance();
//		//calendar.set(Calendar.HOUR,1);
//		calendar.set(Calendar.MINUTE,59);
//		calendar.set(Calendar.SECOND,59);
//		System.out.println(sdf.format(calendar.getTime()));
//		System.out.println(DateTimeUtils.getNowHourStart());
//		System.out.println(DateTimeUtils.getNowHourEnd());
		System.out.println(sdf.format(DateTimeUtils.getNowMonthStart()));
		System.out.println(sdf.format(DateTimeUtils.getNowMonthEnd()));
	}

}

